package com.ms.p2p.controller.admin;


import com.alibaba.dubbo.config.annotation.Reference;
import com.ms.common.bean.Message;
import com.ms.common.utils.MsDateUtils;
import com.ms.p2p.bean.PageBean;
import com.ms.p2p.controller.BaseController;
import com.ms.p2p.entity.content.Advert;
import com.ms.p2p.entity.system.AdminUser;
import com.ms.p2p.provider.AdvertProvider;
import com.ms.p2p.utils.JsonDataGridHelper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 项目图片管理
 */

@Controller("AdminAdvertController")
@RequestMapping({ "/admin/advert" })
public class AdminAdvertController extends AdminBaseController {

	@Reference
	private AdvertProvider advertProvider;

	@RequestMapping("/edit")
	@ResponseBody
	public Message edit(HttpSession session, Advert advert) throws Exception {
		String message = "";
		AdminUser emp = getCurrentEmp();
		advert.setAdd_person(emp.getName());
		advert.setAdd_time(MsDateUtils.getTime());
		if (advert.getId() != null) {
			advertProvider.update(advert);

		} else {
			advertProvider.insert(advert);

		}

		return Message.success("操作成功");

	}

	@RequestMapping(value = "/delete", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Message delPPT(@RequestParam(value = "id") Integer id) {
		advertProvider.delete(id);
		return Message.success("操作成功");
	}

	@RequestMapping({ "/add" })
	public String toUpdatePpt(HttpServletRequest request, @RequestParam(value = "id", required = false) Integer id) {
		if (id != null) {
			Advert advert = advertProvider.selectById(id);
			request.setAttribute("ppt", advert);
		}

		return "admin/advert/add";
	}

	@RequestMapping({ "/list" })
	public String forwardPPT(HttpServletRequest request, HttpSession session) {
		return "admin/advert/list";
	}

	@RequestMapping(value = {"/queryData"}, produces = {"application/json;charset=UTF-8"})
	@ResponseBody
	public PageBean queryData(HttpServletRequest request, HttpSession session
			, @RequestParam(value = "userId", required = false) Integer userId,
								String addDate_begin,
							  String addDate_end,
							  PageBean page
	) {

		page.setTotalCount(advertProvider.findCount(addDate_begin,addDate_end,null,null));
		page.setList(advertProvider.findPager(addDate_begin,addDate_end,null,null,page));
		return JsonDataGridHelper.createJSONData(page);
	}

	@Override
	public boolean getBinderFlag() {
		return false;
	}
}
